package com.skp.shaphan.actions;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.skp.job.ProjectJobException;
import com.skp.shaphan.ui.SQLEditor;
import com.skp.shaphan.ui.results.IResultPane;
import com.skp.shaphan.ui.results.ResultPane;

/**
 * Shows all the tables in a connection
 * @author Stephen Phillips
 *
 */
public class ShowTablesAction implements Runnable {
	private SQLEditor editor;
	private IResultPane resultPane;
	
	public ShowTablesAction() {
		
	}
	
	@Override
	public void run() {
		Connection connection = editor.getConnection().getConnection();
		ResultSet results;
		try {
			ArrayList<String> tableTypes = new ArrayList<String>();
			results = connection.getMetaData().getTableTypes();
			while(results.next()) {
				tableTypes.add(results.getString(1));
			}
			results = connection.getMetaData().getTables(null, null, "%", tableTypes.toArray(new String[0]));
		} catch (SQLException e) {
			(new ProjectJobException(e)).displayException();
			return;
		}
		resultPane.setResultSet("", results);
	}

	public SQLEditor getEditor() {
		return editor;
	}

	public void setEditor(SQLEditor editor) {
		this.editor = editor;
	}

	public IResultPane getResultPane() {
		return resultPane;
	}

	public void setResultPane(IResultPane iResultPane) {
		this.resultPane = iResultPane;
	}

}
